package com.yudaob2b.elong.task;

import com.yudaob2b.business.entity.po.ElongHotelRatePO;
import com.yudaob2b.business.service.ElongHotelRateService;
import com.yudaob2b.common.utils.DateUtil;
import com.yudaob2b.common.utils.ListUtil;
import com.yudaob2b.elong.constant.Constant;
import com.yudaob2b.elong.entity.vo.Rate;
import com.yudaob2b.elong.entity.vo.result.HotelDataRateResult;
import com.yudaob2b.elong.enums.EnumPaymentType;
import com.yudaob2b.elong.service.HotelRateService;
import com.yudaob2b.elong.service.HttpStaticService;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/20/18 8:42 PM
 */
public class DataRateLocalTask implements Runnable, Serializable {
    private static Logger dataRateLocalTaskLogger = LoggerFactory.getLogger("dataRateLocalTaskLogger");

    /**
     * 酒店id，最多10个
     */
    private List<String> hotelIds;
    private HttpStaticService httpStaticService;
    private HotelRateService hotelRateService;
    private ThreadPoolTaskExecutor dataRateDbTaskExecutor;
    private ElongHotelRateService elongHotelRateService;
    private RedisTemplate redisTemplate;
    private ClientInterface clientUtil;
    // 线程添加/更新列表数
    private final int TASK_LIST_COUNT = 500;

    public DataRateLocalTask(List<String> hotelIds, HttpStaticService httpStaticService
            , HotelRateService hotelRateService, ThreadPoolTaskExecutor dataRateDbTaskExecutor
            , ElongHotelRateService elongHotelRateService, RedisTemplate redisTemplate
            , ClientInterface clientUtil) {
        this.hotelIds = hotelIds;
        this.httpStaticService = httpStaticService;
        this.hotelRateService = hotelRateService;
        this.dataRateDbTaskExecutor = dataRateDbTaskExecutor;
        this.elongHotelRateService = elongHotelRateService;
        this.redisTemplate = redisTemplate;
        this.clientUtil = clientUtil;
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();
        if (hotelIds.size() <= 10) {
            // 查询开始时间，当天时间
            Date ratestartDate = new Date();
            // 查询结束时间，90天后的时间
            Date rateEndDate = DateUtil.rollDay(ratestartDate, Constant.INCR_DAYS);

            StringBuilder hotelIdBuilder = new StringBuilder();
            for (String hotelId : hotelIds) {
                hotelIdBuilder.append(hotelId).append(",");
            }

            // 10个酒店id查询一次
            HotelDataRateResult rateResult = httpStaticService.getHotelDataRate(hotelIdBuilder.toString(), null, EnumPaymentType.Prepay, ratestartDate, rateEndDate);
            // 重置查询的酒店id
            if (rateResult != null && "0".equals(rateResult.getCode()) && rateResult.getResult().getRates().size() > 0) {
                List<List<Rate>> listPage = ListUtil.split(rateResult.getResult().getRates(), TASK_LIST_COUNT);
                for (List<Rate> rateList : listPage) {
                    DataRateLocalDbTask dbTask = new DataRateLocalDbTask(rateList, elongHotelRateService, redisTemplate, hotelRateService, clientUtil);
                    dataRateDbTaskExecutor.execute(dbTask);
                }
            }
        }
        //dataRateLocalTaskLogger.info("---价格落地线程总耗时：---{}", System.currentTimeMillis() - start);
    }
}
